package com.li.youhappygo.controller;

import com.github.pagehelper.PageInfo;
import com.li.youhappygo.model.dto.UserDto;
import com.li.youhappygo.model.dto.UserQueryParamDto;
import com.li.youhappygo.model.entity.user.UserAddress;
import com.li.youhappygo.model.entity.user.Users;
import com.li.youhappygo.model.vo.common.Result;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.service.UserAddressService;
import com.li.youhappygo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/user")
@Tag(name="用户信息管理（这些接口已经被客户管理取代）", description = "这里是对用户表信息的CRUD")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    UserAddressService userAddressService;

    @PostMapping("addUser")
    @Operation(summary = "添加用户",description = "添加一个用户")
    public Result addUser(@RequestBody Users user) {
        userService.addUser(user);
        return Result.build(null, ResultCode.SUCCESS);
    }

    @GetMapping("/getUserList/{pageNum}/{pageSize}")
    @Operation(description = "显示所有客户信息，分页查询", summary = "显示所有客户的省略信息")
    public Result<PageInfo<Users>> getUserList(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Users> pageInfo = userService.getUserList(pageNum, pageSize);
        return Result.build(pageInfo, ResultCode.SUCCESS);
    }
    @GetMapping("/getUserInfoById/{id}")
    @Operation(description = "显示单个客户的所有信息",summary = "显示某个客户的具体信息")
    public Result<Users> getUserInfoById(@PathVariable Integer id) {
        Users users = userService.getUserInfoById(id);
        return Result.build(users, ResultCode.SUCCESS);
    }

    @PostMapping("/updateUserInfo")
    @Operation(description = "更新用户信息",summary = "更改某个客户的信息")
    public Result updateUserInfo(@RequestBody UserDto users) {
        userService.updateUserInfo(users);
        return Result.build(null, ResultCode.SUCCESS);
    }
    @GetMapping("/deleteUser/{id}")
    @Operation(description = "删除用户信息",summary = "删除某个客户")
    public Result deleteUser(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.build(null, ResultCode.SUCCESS);
    }

    @GetMapping("/getUserAddressByUsersId/{UsersId}")
    @Operation(description = "获取ID用户的地址列表",summary = "获取某个用户的地址信息")
//    @ApiResponses(@ApiResponse(content = {@Content(schema = @Schema(implementation = UserAddress.class))}))
    public Result<PageInfo<UserAddress>> getUserAddressById(@PathVariable Integer UsersId,Integer pageNum,Integer pageSize) {
        PageInfo<UserAddress> userAddressList = userAddressService.getUserAddressById(UsersId,pageNum,pageSize);
        return Result.build(userAddressList, ResultCode.SUCCESS);
    }
    @GetMapping("/deleteUserAddressById/{id}")
    @Operation(description = "删除ID用户的某个地址信息",summary = "删除某个用户的某个地址信息")
    public Result deleteUserAddressByUsersId(@PathVariable Integer id) {
        userAddressService.deleteUserAddressByUsersId(id);
        return Result.build(null, ResultCode.SUCCESS);
    }
    @PostMapping("/updateUserAddress")
    @Operation(description = "修改用户的某个地址信息",summary = "修改某个用户的某个地址信息")
    public Result updateUserAddress(@RequestBody UserAddress userAddress) {
        userAddressService.updateUserAddress(userAddress);
        return Result.build(null, ResultCode.SUCCESS);
    }
    @PostMapping("/addUserAddress")
    @Operation(description = "添加用户的某个地址信息",summary = "添加某个用户的某个地址信息")
    public Result addUserAddress(@RequestBody UserAddress userAddress) {
        userAddressService.addUserAddress(userAddress);
        return Result.build(null, ResultCode.SUCCESS);
    }

    @PostMapping("getByUserListByQueryParam")
    @Operation(description = "根据查询条件查找用户",summary = "根据查询条件查找用户")
    public Result<PageInfo<Users>> getByUserListByQueryParam(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                             @RequestBody UserQueryParamDto userQueryParamDto){
        PageInfo<Users> pageInfo = userService.getByUserListByQueryParam(userQueryParamDto,pageSize,pageNum);
        return Result.build(pageInfo, ResultCode.SUCCESS);

    }
}
